package com.qf.mybatis01.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class QfFileReader {
    private String filepath;
    private BufferedReader bufferedReader;
    private Integer batchNum=1000;//批次数量，每次提取的数量

    public QfFileReader(String filepath) throws FileNotFoundException {
        this.filepath = filepath;
        this.bufferedReader = new BufferedReader(new FileReader(filepath));
    }

    public QfFileReader(String filepath, Integer batchNum) throws FileNotFoundException {
        this.filepath = filepath;
        this.batchNum = batchNum;
        this.bufferedReader = new BufferedReader(new FileReader(filepath));
    }
    public List<String> readBatch() throws IOException {
        ArrayList<String> list = new ArrayList<>();
        int count=0;
        while(true){
            String line = bufferedReader.readLine();
            if(line!=null){
                list.add(line);
                count++;
                if(count==batchNum){
                    return list;
                }
            }else {
                return list;
            }
        }
    }
    public void close() throws IOException {
        this.bufferedReader.close();
    }

    public static void main(String[] args) throws IOException {
        QfFileReader qfFileReader = new QfFileReader("C:\\test\\books.txt",3);
        while (true) {
            List<String> list = qfFileReader.readBatch();
            if((list.size()==0)){
                System.out.println("finish!");
                return;
            }
            for (String s : list) {
                System.out.println(s);
            }
            System.out.println("====================");
        }
    }
}
